<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Chat Demo</title>
</head>
<body>
<div id="container">
    <ul>
        <li v-for="msg in messages">[<span style="color:red;">{{msg.name}}</span>]{{msg.content}}</li>
    </ul>
    <div class="controls">
        <input type="text" v-model="nickname">
        <input type="text" v-model="inputMessage">
        <input type="button" v-on:click="sendMessage" value="Send">
    </div>
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js" type="text/javascript"></script>
<!--[starx websocket library](https://github.com/topfreegames/pitaya-client-websocket)-->
<script src="protocol.js" type="text/javascript"></script>
<script src="starx-wsclient.js" type="text/javascript"></script>
<script>
    var v = new Vue({
        el: "#container",
        data: {
            nickname:'guest' + Date.now(),
            inputMessage:'',
            messages: []
        },
        methods: {
            sendMessage: function () {
                console.log(this.inputMessage);
                starx.notify('room.message', {name: this.nickname, content: this.inputMessage});
                this.inputMessage = '';
            }
        }
    });

    var onMessage = function (msg) {
        v.messages.push(msg)
    };

    var join = function (data) {
        console.log(data);
        if(data.code === 0) {
            v.messages.push({name:'system', content:data.result});
            starx.on('onMessage', onMessage)
        }
    };

    var onNewUser = function (data) {
        console.log(data);
        v.messages.push({name:'system', content:data.content});
    };

    var onMembers = function (data) {
        console.log(data);
        v.messages.push({name:'system', content: "members: "+data.members});
    };

    starx.init({host: '127.0.0.1', port: 3250, path: '/'}, function () {
        console.log("initialized");
        starx.on("onNewUser", onNewUser);
        starx.on("onMembers", onMembers);
        starx.request("room.join", {}, join);
    })
</script>
</body>
</html>
